package com.lagou.edu.utils;



import com.alibaba.druid.support.json.JSONUtils;
import com.lagou.edu.factory.BeanFactory;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PackageScanner {

    private List<Class<?>> classes;

    private Map<String, Class<?>> classMap;

    //com.lagou.edu
    private String packageName;
    //D:\Study\ProgrammingProject\IDEAProject\JavaWebLagouLearn\J1M2_210207_homework\lagou-transfer\src\main\java\com\lagou\edu
    private String packagePath;
    //D:\Study\ProgrammingProject\IDEAProject\JavaWebLagouLearn\J1M2_210207_homework\lagou-transfer\src\main\java
    private String modulePath;

    public List<Class<?>> getClasses() {
        return classes;
    }

    public Map<String, Class<?>> getClassMap() {
        return classMap;
    }

    public String getPackageName() {
        return packageName;
    }

    public String getPackagePath() {
        return packagePath;
    }


    /**
     *
     * @param packageName com.lagou.edu
     */
    public PackageScanner(String packageName) {
        classes = new ArrayList<>();
        classMap = new HashMap<>();
        this.packageName = packageName;
//        this.packagePath = System.getProperty("user.dir") + "\\lagou-transfer" + "\\src\\main\\java\\" + packageName.replace('.', '\\');
//        this.modulePath = System.getProperty("user.dir") + "\\lagou-transfer" + "\\src\\main\\java";
        this.packagePath = System.getProperty("user.dir") + "\\src\\main\\java\\" + packageName.replace('.', '\\');
        this.modulePath = System.getProperty("user.dir") + "\\src\\main\\java";

        System.out.println(packagePath);
        System.out.println(modulePath);
    }
    //正式开始扫描包下所有类
    public void doFileScan() {
        this.fileScanner(new File(packagePath));
    }

    public void fileScanner(File file) {
        //如果是文件夹，就递归寻找文件
        //控制不扫描servlet
        if (file.isDirectory() && !file.getName().equals("servlet")) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File item : files) {
                    fileScanner(item);
                }
            }
        }

        //如果是文件，就放入classes集合中
        else {
            //只扫描.java文件
            if (file.getName().matches(".+\\.(java)")) {

//                System.out.println(file.getName());
                String fileName = file.getName();
                String fileAbsPath = file.getAbsolutePath();
                // com\lagou\edu\anno\Autowired
                String relativePath = fileAbsPath.substring(modulePath.length() + 1, fileAbsPath.length() - 5);
//                System.out.println(relativePath);
                // com.lagou.edu.dao.impl.JdbcAccountDaoImpl
                String relativeReference = relativePath.replace("\\", ".");
//                System.out.println(relativeReference);

                try {
//                    classes.add(Class.forName(this.packageName + "." + file.getName().substring(0, file.getName().length() - 5)));
                    classes.add(Class.forName(relativeReference));

                    classMap.put(JsonUtils.classNameToCamelName(fileName.substring(0, fileName.length() - 5)), Class.forName(relativeReference));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
